import * as ECharts from "echarts";
import elementResizeDetectorMaker from "element-resize-detector";

const HANDLER = "_vue_resize_handler";

const chartResize = {
  mounted(el, binding) {
    el[HANDLER] = binding.value
      ? binding.value
      : () => {
          let chart = ECharts.getInstanceByDom(el);
          if (!chart) {
            return;
          }
          chart.resize();
        };
    elementResizeDetectorMaker().listenTo(el, el[HANDLER]);
  },
  unmounted(el) {
    elementResizeDetectorMaker().removeListener(el, el[HANDLER]);
    delete el[HANDLER];
  }
};

export default chartResize;